package leetcode_401_500;

public class LeeCode_419 {
    public static void main(String[] args) {
        System.out.println(countBattleships(new char[][]{{'X', '.', '.', 'X'}, {'.', '.', '.', 'X'}, {'.', '.', '.', 'X'}, {'.', '.', '.', '.'}}));
    }
    private static int countBattleships(char[][] board) {
        int m = board.length, n = board[0].length, ans = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 'X'){
                    ans++;
                    int t = j + 1;
                    while (t < n && board[i][t] == 'X'){
                        board[i][t] = '.';
                        t++;
                    }
                    t = i + 1;
                    while (t < m && board[t][j] == 'X'){
                        board[t][j] = '.';
                        t++;
                    }
                    board[i][j] = '.';
                }
            }
        }
        return ans;
    }
}
